<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\VisitLog\Retry;
use App\Models\Device;
use App\Models\VisitLog;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class VisitLogController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'VisitLog';


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new VisitLog());
        $grid->actions(function ($actions) {
            if ($actions->row['status']==VisitLog::STATUS_CREATED){
                $actions->add(new Retry());
            }
        });
        $grid->model()->orderBy('id', 'desc');

        $grid->column('id', __('Id'));
        $grid->column('device_id', __('Device id'));
        $grid->column('device_code', __('Device code'));
        $grid->column('started_at', __('Started at'));
//        $grid->column('created_at', __('Created at'));
//        $grid->column('updated_at', __('Updated at'));
        $grid->column('finished_at', __('Finished at'));
        $grid->column('user_id', __('User id'));
        $grid->column('original_url', __('Original url'));
        $grid->column('final_url', __('Final url'));
        $grid->column('file', '截图文件');
        $grid->column('status', __('Status'));
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $filter->equal('local_device_id', __('Local device id'))->select(Device::all()->pluck('full_tag','id'));

            $filter->equal('device_code', __('Device code'));
            $filter->equal('status', __('Status'))->select(VisitLog::STATUS_VALUE);
            $filter->between('started_at', __('Started at'))->datetime();
            $filter->between('finished_at', __('Finished at'))->datetime();
//            $filter->between('created_at', __('Created at'))->datetime();
//            $filter->between('updated_at', __('Updated at'))->datetime();
            $filter->equal('original_url', __('Original url'));
            $filter->equal('final_url', __('Final url'));
//            $filter->between('location_at', __('Location at'))->datetime();;

        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(VisitLog::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('device_id', __('Device id'));
        $show->field('device_code', __('Device code'));
        $show->field('started_at', __('Started at'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('finished_at', __('Finished at'));
        $show->field('user_id', __('User id'));
        $show->field('original_url', __('Original url'));
        $show->field('final_url', __('Final url'));
        $show->field('file', '截图文件');
        $show->field('status', __('Status'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new VisitLog());

        $models = Device::all();
        $list = [];
        foreach ($models as $model) {
            $list[$model->id] = "ID:$model->id 名称:$model->name 编码:$model->code";

        }
        $form->select('device_id', __('Device id'))->options($list)->required();
        $form->datetime('started_at', __('Started at'))->default(date('Y-m-d H:i:s'))->required();
        $form->datetime('finished_at', __('Finished at'))->default(date('Y-m-d H:i:s'));
        $form->text('user_id', __('User id'))->required();
        $form->url('original_url', __('Original url'))->required();
        $form->url('final_url', __('Final url'));
        $form->url('file', '截图文件');
        $form->radio('status', __('Status'))->options(VisitLog::STATUS_VALUE)->default(VisitLog::STATUS_CREATED);

        return $form;
    }
}
